package HTML;

use strict;
use @@PACKAGE@@::WebAPI::Util;

sub new {

    # Get the class name
    my $stInvocant = shift @_;
    my $stClass = ref($stInvocant) || $stInvocant;

    # Create the current instance and bless into class
    my $rSelf = { };
    bless($rSelf,$stClass);

    $rSelf->{title} = shift @_;
    my %hAttr = ( @_ );

    $rSelf->{children} = [];
    $rSelf->{num_children} = 0;
    $rSelf->{html_attr} = \%hAttr;
    $rSelf->{body_attr} = { };

    return $rSelf;
}

sub setBodyAttr {

    my $rSelf = shift(@_);
    my %hBodyAttr = ( @_ );
    $rSelf->{body_attr} = \%hBodyAttr;
    
    return 1;
}

sub addChild {

    my $rSelf = shift(@_);
    my $rChild = shift(@_);
    
    if ( defined($rChild) ) {

	$rSelf->{children}->[$rSelf->{num_children}] = $rChild;
	$rSelf->{num_children}++;

    }
    
    return 1;
}

sub draw {

    my $rSelf = shift(@_);
    my $stResult = "";

    $stResult .= "<html ". Util::get_attr_str($rSelf->{html_attr}) . ">\n";
    $stResult .= $rSelf->draw_head();

    $stResult .= "<body". Util::get_attr_str($rSelf->{body_attr}) . ">";

    my $i = 0;    
    for $i (0 .. $rSelf->{num_children}-1) {

	if ( defined( $rSelf->{children}->[$i] ) ) {

	    $stResult .= 
		ref( $rSelf->{children}->[$i] ) ?
		$rSelf->{children}->[$i]->draw() :
		$rSelf->{children}->[$i];
	}
    }

    $stResult .= "</body>\n</html>\n";

    return $stResult;
}

sub draw_head {

    my $rSelf = shift(@_);
    my $stResult = "";

    $stResult .= "<head>\n";
    $stResult .= "<title>$rSelf->{title}</title>\n";
    $stResult .= "</head>\n";
    
    return $stResult;
}

1;
